package com.qf.staff.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.FileUploadUtils;
import com.qf.common.db.utils.PageCommonBeanUtils;
import com.qf.staff.common.qo.HomeQo;
import com.qf.staff.common.vo.HomeVo;
import com.qf.staff.mapper.HomeMapper;
import com.qf.staff.pojo.Home;
import com.qf.staff.service.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Resource
    HomeMapper homeMapper;
    @Resource
    private FileUploadUtils fileUploadService;

    @Override
    public IPage<HomeVo> list(int page, int size) {
        try {
            return PageCommonBeanUtils.copyPage(homeMapper.selectPage(new Page<>(page, size), null),
                    new Page<>(), HomeVo::new);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Override
    public int delete(HomeQo homeQo) {
        try {
            Home home = new Home();
            BeanUtils.copyProperties(homeQo, home);
            return homeMapper.deleteById(home);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }


    @Override
    public int modify(HomeQo homeQo) {
        try {
            Home home = new Home();
            BeanUtils.copyProperties(homeQo, home);
            return homeMapper.updateById(home);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public int add(HomeQo homeQo) {
        try {
            Home home = new Home();
            BeanUtils.copyProperties(homeQo, home);
            return homeMapper.insert(home);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }
}
